package samples.d2.lines 
{
	import adobe.utils.CustomActions;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import shapes.shapes2D.curves.RoundPath;
	import shapes.shapes2D.Dot;
	import shapes.shapes2D.Line;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Lines5 extends BaseSample
	{
		
		private var p0:Point;
		private var p1:Point;
		private var maxDistance:int = 5;
		private var angle:Number = 0;
		private var mouseDown:Boolean;
		public function Lines5() 
		{
			
			
			p0 = new Point( sw/2, sh / 2 );
			filters = [new GlowFilter( 0xA8F9FD, 1, 16,16, 3 ) ];
			interval = setInterval( update, 30 );
			
			_stage.addEventListener( MouseEvent.MOUSE_DOWN, blitz );
			_stage.addEventListener( MouseEvent.MOUSE_UP, blitz );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemove );
		}
		
		private function onRemove(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			_stage.removeEventListener( MouseEvent.MOUSE_DOWN, blitz );
			
		}
		
		private function blitz(e:MouseEvent):void 
		{
			mouseDown = ( e.type == MouseEvent.MOUSE_DOWN );
		}
		
		private function update():void 
		{
			
			color = 0xFFFFFF;
			
			graphics.clear();
			
			graphics.lineStyle( 0, color );
			p0.x = sw / 2 + Math.cos( angle ) * 250;
			p0.y = sh / 2 + Math.sin( angle * 1.25 ) * 250;
			angle += Math.PI / 180 * 3;
			graphics.beginFill( color );
			graphics.drawCircle( p0.x, p0.y, 10 );
			graphics.endFill();
			
			if ( mouseDown )
			{
				p1 = new Point( mouseX, mouseY );
				
				maxDistance = 5;
				eclair( p0, p1, 8 );
				eclair( p0, p1, 5 );
				eclair( p0, p1, 9 );
				maxDistance = 8;
				eclair( p0, p1, 7 );
				//eclair( p0, p1, 9 );
			}
		}
		private function eclair( p0:Point, p1:Point, iteration:int ):void
		{
			iteration--;
			
			if ( iteration > 0 )
			{	
				var middle:Point = new Point();
				middle.x = ( p1.x + p0.x ) * .5 + ( (Math.random() - .5) * ( maxDistance * .5 ) ) * iteration;
				middle.y = ( p1.y + p0.y ) * .5 + ( (Math.random() - .5) * maxDistance ) * iteration;
				
				eclair( p0, middle, iteration );
				eclair( middle, p1, iteration );
				
			}
			else
			{
				
				graphics.moveTo( p0.x, p0.y );
				graphics.lineTo( p1.x, p1.y );
				
			}
		}
		
	}

}